package com.jwdf.uda.ui.fragment.question;

import android.Manifest;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.databinding.FragmentNewVoteBinding;
import com.jwdf.uda.event.NewVoteEvent;
import com.jwdf.uda.runtime.PermissionRequest;
import com.jwdf.uda.runtime.RuntimePermission;
import com.jwdf.uda.selector.ImageSelectorItem;
import com.jwdf.uda.ui.activity.presenter.NewVotePresenter;
import com.jwdf.uda.ui.activity.view.INewVoteView;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.question.bean.NewVote;
import com.jwdf.uda.ui.fragment.question.bean.NewVoteList;
import com.jwdf.uda.utils.EventBusUtil;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/2.
 */
public class NewVoteFragment extends BaseFragment implements INewVoteView,View.OnClickListener {
    private FragmentNewVoteBinding mBinding;
    private NewVotePresenter mPresenter;
    private List<NewVote> mNewVoteList;
    private NewVoteList mNewProblems;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_new_vote, container, false);
        initView();

        return mBinding.getRoot();
    }

    private void initView() {
        checkPermission();
        mPresenter = new NewVotePresenter(this);
        EventBusUtil.registerEvent(this);
        EventBusUtil.registerEvent(mBinding.isVoteAImages);
        EventBusUtil.registerEvent(mBinding.isVoteBImages);
        EventBusUtil.registerEvent(mBinding.isVoteCImages);
        EventBusUtil.registerEvent(mBinding.isVoteDImages);


        mBinding.voteAddBtn.setOnClickListener(this);
        mBinding.ivVoteADelete.setOnClickListener(this);
        mBinding.ivVoteBDelete.setOnClickListener(this);
        mBinding.ivVoteCDelete.setOnClickListener(this);
        mBinding.ivVoteDDelete.setOnClickListener(this);

        Bundle bundle=getArguments();
        mNewProblems=(NewVoteList) bundle.getSerializable("NewVoteList");
        if(mNewProblems!=null){
           initEditorVote();
        }

    }

    /**
     * 编辑投票显示内容
     */
    public void initEditorVote(){
        mNewVoteList=mNewProblems.getNewVoteList();
        initVote(mNewVoteList);
    }

    public void initVote(List<NewVote> mNewVoteList){
        if (mNewVoteList.get(0).getDescription().length()>0) {
            List<ImageSelectorItem> items = new ArrayList<>();
            for(int j=0;j<mNewVoteList.get(0).getUploadImages().size();j++) {
                ImageSelectorItem item = new ImageSelectorItem();
                item.imageUrl = mNewVoteList.get(0).getUploadImages().get(j).getImageUrl();
                item.imageToken = mNewVoteList.get(0).getUploadImages().get(j).getImageToken();
                items.add(item);
            }
            mBinding.llVoteA.setVisibility(View.VISIBLE);
            mBinding.isVoteAImages.setImageList(items);
            mBinding.editVoteA.setText(mNewVoteList.get(0).getDescription());
        }
        if (mNewVoteList.get(1).getDescription().length()>0) {
            List<ImageSelectorItem> items = new ArrayList<>();
            for(int j=0;j<mNewVoteList.get(1).getUploadImages().size();j++) {
                ImageSelectorItem item = new ImageSelectorItem();
                item.imageUrl = mNewVoteList.get(1).getUploadImages().get(j).getImageUrl();
                item.imageToken = mNewVoteList.get(1).getUploadImages().get(j).getImageToken();
                items.add(item);
            }
            mBinding.llVoteB.setVisibility(View.VISIBLE);
            mBinding.isVoteBImages.setImageList(items);
            mBinding.editVoteB.setText(mNewVoteList.get(1).getDescription());
        }
        if (mNewVoteList.get(2).getDescription().length()>0) {
            List<ImageSelectorItem> items = new ArrayList<>();
            for(int j=0;j<mNewVoteList.get(2).getUploadImages().size();j++) {
                ImageSelectorItem item = new ImageSelectorItem();
                item.imageUrl = mNewVoteList.get(2).getUploadImages().get(j).getImageUrl();
                item.imageToken = mNewVoteList.get(2).getUploadImages().get(j).getImageToken();
                items.add(item);
            }
            mBinding.llVoteC.setVisibility(View.VISIBLE);
            mBinding.isVoteCImages.setImageList(items);
            mBinding.editVoteC.setText(mNewVoteList.get(2).getDescription());
        }
        if (mNewVoteList.get(3).getDescription().length()>0) {
            List<ImageSelectorItem> items = new ArrayList<>();
            for(int j=0;j<mNewVoteList.get(3).getUploadImages().size();j++) {
                ImageSelectorItem item = new ImageSelectorItem();
                item.imageUrl = mNewVoteList.get(3).getUploadImages().get(j).getImageUrl();
                item.imageToken = mNewVoteList.get(3).getUploadImages().get(j).getImageToken();
                items.add(item);
            }
            mBinding.llVoteD.setVisibility(View.VISIBLE);
            mBinding.isVoteDImages.setImageList(items);
            mBinding.editVoteD.setText(mNewVoteList.get(3).getDescription());
            mBinding.voteAddBtn.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregisterEvent(this);
        EventBusUtil.unregisterEvent(mBinding.isVoteAImages);
        EventBusUtil.unregisterEvent(mBinding.isVoteBImages);
        EventBusUtil.unregisterEvent(mBinding.isVoteCImages);
        EventBusUtil.unregisterEvent(mBinding.isVoteDImages);
    }

    @PermissionRequest(100)
    private void checkPermission() {
        String perms[] = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
        if (!RuntimePermission.hasPermissions(getContext(), perms)) {
            RuntimePermission.requestPermissions(this, getString(R.string.app_name),
                    100, perms);
        }
    }

    @Override
    public String getContent() {
//        String content = StringUtils.filterNullAndTrim(mBinding.etContent.getText().toString());
//        if (StringUtils.isNullEmpty(content)) {
//            return getString(R.string.vote_add_content_def);
//        }
//        return content;
        return "";
    }

    public void onEventMainThread(NewVoteEvent event) {
        switch (event.getEvent()){
            case NewVoteEvent.EVENT_NEW_VOTE_SAVE:
                mPresenter.onHeadActionClick();
                break;
            case NewVoteEvent.EVENT_NEW_VOTE_EDITOR:
                mNewVoteList=event.getNewVoteList();
                break;
        }

    }

    @Override
    public List<UploadImage> getUploadImageList(int plan) {
        switch (plan) {
            case NewVotePresenter.PLAN_A:
                return mBinding.isVoteAImages.getUploadImageList();
            case NewVotePresenter.PLAN_B:
                return mBinding.isVoteBImages.getUploadImageList();
            case NewVotePresenter.PLAN_C:
                return mBinding.isVoteCImages.getUploadImageList();
            case NewVotePresenter.PLAN_D:
                return mBinding.isVoteDImages.getUploadImageList();
        }
        return null;
    }

    @Override
    public String getDescription(int plan) {
        switch (plan) {
            case NewVotePresenter.PLAN_A:
                return mBinding.editVoteA.getText().toString();
            case NewVotePresenter.PLAN_B:
                return mBinding.editVoteB.getText().toString();
            case NewVotePresenter.PLAN_C:
                return mBinding.editVoteC.getText().toString();
            case NewVotePresenter.PLAN_D:
                return mBinding.editVoteD.getText().toString();
        }
        return null;
    }

    @Override
    public List<NewVote> getNewVoteList() {
        NewVote newVoteA=new NewVote();
        newVoteA.setUploadImages(getUploadImageList(NewVotePresenter.PLAN_A));
        newVoteA.setDescription(getDescription(NewVotePresenter.PLAN_A));
        NewVote newVoteB=new NewVote();
        newVoteB.setDescription(getDescription(NewVotePresenter.PLAN_B));
        newVoteB.setUploadImages(getUploadImageList(NewVotePresenter.PLAN_B));
        NewVote newVoteC=new NewVote();
        newVoteC.setDescription(getDescription(NewVotePresenter.PLAN_C));
        newVoteC.setUploadImages(getUploadImageList(NewVotePresenter.PLAN_C));
        NewVote newVoteD=new NewVote();
        newVoteD.setDescription(getDescription(NewVotePresenter.PLAN_D));
        newVoteD.setUploadImages(getUploadImageList(NewVotePresenter.PLAN_D));
        List<NewVote> list=new ArrayList<>();
        list.add(newVoteA);
        list.add(newVoteB);
        list.add(newVoteC);
        list.add(newVoteD);
        return list;
    }



    @Override
    public void setImageToken(int plan, UploadImage uploadImage) {
        switch (plan) {
            case NewVotePresenter.PLAN_A:
                mBinding.isVoteAImages.setImageToken(uploadImage);
                break;
            case NewVotePresenter.PLAN_B:
                mBinding.isVoteBImages.setImageToken(uploadImage);
                break;
            case NewVotePresenter.PLAN_C:
                mBinding.isVoteCImages.setImageToken(uploadImage);
            case NewVotePresenter.PLAN_D:
                mBinding.isVoteDImages.setImageToken(uploadImage);
                break;
        }
    }

    @Override
    public boolean isVoteCVisibility() {
        return mBinding.llVoteC.getVisibility() == View.VISIBLE;
    }

    @Override
    public boolean isVoteDVisibility() {
        return mBinding.llVoteD.getVisibility() == View.VISIBLE;
    }

    @Override
    public boolean isVoteBVisibility() {
        return mBinding.llVoteB.getVisibility() == View.VISIBLE;
    }

    @Override
    public boolean isVoteAVisibility() {
        return mBinding.llVoteA.getVisibility() == View.VISIBLE;
    }

    @Override
    public BaseFragment getFragment() {
        return this;
    }

    @Override
    public void onHeadActionClick() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_vote_a_delete:
                onClickDeleteVote(0);
                break;
            case R.id.iv_vote_b_delete:
                onClickDeleteVote(1);
                break;
            case R.id.iv_vote_c_delete:
                onClickDeleteVote(2);
                break;
            case R.id.iv_vote_d_delete:
                onClickDeleteVote(3);
                break;
            case R.id.vote_add_btn:
                if(mBinding.llVoteC.getVisibility()==View.VISIBLE){
                    mBinding.llVoteD.setVisibility(View.VISIBLE);
                    mBinding.voteAddBtn.setVisibility(View.GONE);
                }else if(mBinding.llVoteB.getVisibility()==View.VISIBLE){
                    mBinding.llVoteC.setVisibility(View.VISIBLE);
                }else if(mBinding.llVoteA.getVisibility()==View.VISIBLE){
                    mBinding.llVoteB.setVisibility(View.VISIBLE);
                }else {
                    mBinding.llVoteA.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

    public void onClickDeleteVote(int i){
        NewVote newVoteA=new NewVote();
        newVoteA.setUploadImages(getUploadImageList(NewVotePresenter.PLAN_A));
        newVoteA.setDescription(getDescription(NewVotePresenter.PLAN_A));
        NewVote newVoteB=new NewVote();
        newVoteB.setDescription(getDescription(NewVotePresenter.PLAN_B));
        newVoteB.setUploadImages(getUploadImageList(NewVotePresenter.PLAN_B));
        NewVote newVoteC=new NewVote();
        newVoteC.setDescription(getDescription(NewVotePresenter.PLAN_C));
        newVoteC.setUploadImages(getUploadImageList(NewVotePresenter.PLAN_C));
        NewVote newVoteD=new NewVote();
        newVoteD.setDescription(getDescription(NewVotePresenter.PLAN_D));
        newVoteD.setUploadImages(getUploadImageList(NewVotePresenter.PLAN_D));
        List<NewVote> newVoteList=getNewVoteList();
        List<UploadImage> uploadImageList=new ArrayList<>();
        List<ImageSelectorItem> items = new ArrayList<>();
        NewVote newVote=new NewVote();
        newVote.setDescription("");
        newVote.setUploadImages(uploadImageList);
        if(i==3){
//            newVoteList.add(3,newVote);
            newVoteList.set(3,newVote);
//            newVoteList.remove(3);
            initVote(newVoteList);
            mBinding.llVoteD.setVisibility(View.GONE);
            mBinding.isVoteDImages.setImageList(items);
            mBinding.editVoteD.setText("");
        }
        if(i==2){
            if(mBinding.llVoteD.getVisibility()==View.VISIBLE){
                newVoteList.set(2,newVoteD);
                newVoteList.set(3,newVote);
                initVote(newVoteList);
                mBinding.llVoteD.setVisibility(View.GONE);
                mBinding.isVoteDImages.setImageList(items);
                mBinding.editVoteD.setText("");
            }else {
                newVoteList.set(2,newVote);
                initVote(newVoteList);
                mBinding.llVoteC.setVisibility(View.GONE);
                mBinding.isVoteCImages.setImageList(items);
                mBinding.editVoteC.setText("");
            }
        }
        if(i==1){
            if(mBinding.llVoteD.getVisibility()==View.VISIBLE){
                newVoteList.set(1,newVoteC);
                newVoteList.set(2,newVoteD);
                newVoteList.set(3,newVote);
                initVote(newVoteList);
                mBinding.llVoteD.setVisibility(View.GONE);
                mBinding.isVoteDImages.setImageList(items);
                mBinding.editVoteD.setText("");
            }else if(mBinding.llVoteC.getVisibility()==View.VISIBLE){
                newVoteList.set(1,newVoteC);
                newVoteList.set(2,newVote);
                newVoteList.set(3,newVote);
                initVote(newVoteList);
                mBinding.llVoteC.setVisibility(View.GONE);
                mBinding.isVoteCImages.setImageList(items);
                mBinding.editVoteC.setText("");
            }else {
                newVoteList.set(1,newVote);
                initVote(newVoteList);
                mBinding.llVoteB.setVisibility(View.GONE);
                mBinding.isVoteBImages.setImageList(items);
                mBinding.editVoteB.setText("");
            }
        }
        if(i==0){
            if(mBinding.llVoteD.getVisibility()==View.VISIBLE){
                newVoteList.set(0,newVoteB);
                newVoteList.set(1,newVoteC);
                newVoteList.set(2,newVoteD);
                newVoteList.set(3,newVote);
                initVote(newVoteList);
                mBinding.llVoteD.setVisibility(View.GONE);
                mBinding.isVoteDImages.setImageList(items);
                mBinding.editVoteD.setText("");
            }else if(mBinding.llVoteC.getVisibility()==View.VISIBLE){
                newVoteList.set(0,newVoteB);
                newVoteList.set(1,newVoteC);
                newVoteList.set(2,newVote);
                newVoteList.set(3,newVote);
                initVote(newVoteList);
                mBinding.llVoteC.setVisibility(View.GONE);
                mBinding.isVoteCImages.setImageList(items);
                mBinding.editVoteC.setText("");

            }else if(mBinding.llVoteB.getVisibility()==View.VISIBLE){
                newVoteList.set(0,newVoteB);
                newVoteList.set(1,newVote);
                newVoteList.set(2,newVote);
                newVoteList.set(3,newVote);
                initVote(newVoteList);
                mBinding.llVoteB.setVisibility(View.GONE);
                mBinding.isVoteBImages.setImageList(items);
                mBinding.editVoteB.setText("");
            }else {
                newVoteList.set(0,newVote);
                newVoteList.set(1,newVote);
                newVoteList.set(2,newVote);
                newVoteList.set(3,newVote);
                initVote(newVoteList);
                mBinding.llVoteA.setVisibility(View.GONE);
                mBinding.isVoteAImages.setImageList(items);
                mBinding.editVoteA.setText("");
            }
        }
        mBinding.voteAddBtn.setVisibility(View.VISIBLE);
    }
}
